package com.business.exam.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.business.common.utils.StringUtils;
import com.business.common.utils.spring.SpringUtils;
import com.business.exam.domain.ExamQbCategoryTreeSelect;
import com.business.exam.utils.GeneratorSnowflake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.business.exam.mapper.ExamQbCategoryMapper;
import com.business.exam.domain.ExamQbCategory;
import com.business.exam.service.IExamQbCategoryService;

/**
 * 题库分类Service业务层处理
 *
 * @author zhanglele
 * @date 2023-11-11
 */
@Service
public class ExamQbCategoryServiceImpl implements IExamQbCategoryService
{
    @Autowired
    private ExamQbCategoryMapper examQbCategoryMapper;
    @Autowired
    private GeneratorSnowflake generatorSnowflake;

    /**
     * 查询题库分类
     *
     * @param categoryId 题库分类主键
     * @return 题库分类
     */
    @Override
    public ExamQbCategory selectExamQbCategoryByCategoryId(Long categoryId)
    {
        return examQbCategoryMapper.selectExamQbCategoryByCategoryId(categoryId);
    }

    /**
     * 查询题库分类列表
     *
     * @param examQbCategory 题库分类
     * @return 题库分类
     */
    @Override
    public List<ExamQbCategory> selectExamQbCategoryList(ExamQbCategory examQbCategory)
    {
        return examQbCategoryMapper.selectExamQbCategoryList(examQbCategory);
    }


    /**
     * 查询题库分类树结构信息
     *
     * @param examQbCategory 题库分类信息
     * @return 部门树信息集合
     */
    @Override
    public List<ExamQbCategoryTreeSelect> selectCategoryTreeList(ExamQbCategory examQbCategory)
    {
        List<ExamQbCategory> examQbCategories = examQbCategoryMapper.selectExamQbCategoryList(examQbCategory);
        return buildQbCategoryTreeSelect(examQbCategories);
    }

    /**
     * 构建前端所需要树结构
     *
     * @param categories 题库分类列表
     * @return 树结构列表
     */
    @Override
    public List<ExamQbCategory> buildQbCategoryTree(List<ExamQbCategory> categories)
    {
        List<ExamQbCategory> returnList = new ArrayList<ExamQbCategory>();
        List<Long> tempList = categories.stream().map(ExamQbCategory::getCategoryId).collect(Collectors.toList());
        for (ExamQbCategory category : categories)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(category.getParentId()))
            {
                recursionFn(categories, category);
                returnList.add(category);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = categories;
        }
        return returnList;
    }

    /**
     * 构建前端所需要下拉树结构
     *
     * @param categories 题库分类列表
     * @return 下拉树结构列表
     */
    @Override
    public List<ExamQbCategoryTreeSelect> buildQbCategoryTreeSelect(List<ExamQbCategory> categories)
    {
        List<ExamQbCategory> categoryTrees = buildQbCategoryTree(categories);
        return categoryTrees.stream().map(ExamQbCategoryTreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<ExamQbCategory> list, ExamQbCategory t)
    {
        // 得到子节点列表
        List<ExamQbCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ExamQbCategory tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<ExamQbCategory> getChildList(List<ExamQbCategory> list, ExamQbCategory t)
    {
        List<ExamQbCategory> tlist = new ArrayList<ExamQbCategory>();
        Iterator<ExamQbCategory> it = list.iterator();
        while (it.hasNext())
        {
            ExamQbCategory n = (ExamQbCategory) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getCategoryId().longValue())
            {
                tlist.add(n);
            }
        }
        tlist.sort((u1, u2) -> u1.getOrderNum().compareTo(u2.getOrderNum()));
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ExamQbCategory> list, ExamQbCategory t)
    {
        return getChildList(list, t).size() > 0;
    }


    /**
     * 新增题库分类
     *
     * @param examQbCategory 题库分类
     * @return 结果
     */
    @Override
    public int insertExamQbCategory(ExamQbCategory examQbCategory)
    {
        examQbCategory.setCategoryCode(generatorSnowflake.snowflakeId());
        return examQbCategoryMapper.insertExamQbCategory(examQbCategory);
    }

    /**
     * 修改题库分类
     *
     * @param examQbCategory 题库分类
     * @return 结果
     */
    @Override
    public int updateExamQbCategory(ExamQbCategory examQbCategory)
    {
        return examQbCategoryMapper.updateExamQbCategory(examQbCategory);
    }

    /**
     * 批量删除题库分类
     *
     * @param categoryIds 需要删除的题库分类主键
     * @return 结果
     */
    @Override
    public int deleteExamQbCategoryByCategoryIds(Long[] categoryIds)
    {
        return examQbCategoryMapper.deleteExamQbCategoryByCategoryIds(categoryIds);
    }

    /**
     * 删除题库分类信息
     *
     * @param categoryId 题库分类主键
     * @return 结果
     */
    @Override
    public int deleteExamQbCategoryByCategoryId(Long categoryId)
    {
        return examQbCategoryMapper.deleteExamQbCategoryByCategoryId(categoryId);
    }
}
